package main

import (
	"github.com/gin-gonic/gin"
	"log"
)

func main() {

	engine := gin.Default()

	// Get 请求 ShouldBindQuery
	engine.GET("/getRegister", func(context *gin.Context) {

		var register Register

		if err := context.ShouldBindQuery(&register); err != nil {
			log.Fatal(err.Error())
		}

		username := register.UserName
		phone := register.Phone

		context.Writer.Write([]byte("shouldBindQuery....用户名: " + username + " 电话: " + phone))

	})

	// formData ShouldBind
	engine.POST("/register", func(context *gin.Context) {

		var register Register

		if err := context.ShouldBind(&register); err != nil {
			log.Fatal(err.Error())
			return
		}

		username := register.UserName
		phone := register.Phone

		context.Writer.Write([]byte("shouldBind...注册成功的用户名: " + username + " 电话: " + phone))

	})

	// Json ShouldBindJSON
	engine.POST("/addRegister", func(context *gin.Context) {

		var register Register

		if err := context.ShouldBindJSON(&register); err != nil {
			log.Fatal(err.Error())
		}

		username := register.UserName
		phone := register.Phone

		context.Writer.Write([]byte("shouldBindJson...添加的用户名: " + username + " 电话: " + phone))

	})


	if err := engine.Run(":8080"); err != nil {
		log.Fatal(err.Error())
	}


}

type Register struct {
	UserName string `form:"username"`
	Password string `form:"password"`
	Phone    string `form:"phone"`
}
